local skel = fk.CreateSkill {
  name = "emo__suoxin",
}

Fk:loadTranslationTable{
  ["emo__suoxin"] = "锁心",
  [":emo__suoxin"] = "一名角色的首个回合开始时，你可令其当前手牌视为【铁索连环】。每当处于连环状态的角色受到伤害时，你可以废除其一个装备栏，防止此伤害。",
  ["#emo__suoxin-change"] = "锁心：你可以令 %src 当前手牌视为【铁索连环】",
  ["#emo__suoxin-reset"] = "锁心：你可以废除 %src 的1个装备栏，令其免受 %arg 点伤害",
  ["#emo__suoxin-slot"] = "锁心：选择 %src 的1个装备栏废除",
  ["@@emo__suoxin-inhand"] = "锁心",
}

skel:addEffect(fk.TurnStart, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and not target.dead and not target:isKongcheng() then
      local current_turn = player.room.logic:getCurrentEvent():findParent(GameEvent.Turn, true)
      if current_turn then
        local first_turn = player.room.logic:getEventsOfScope(GameEvent.Turn, 1, function (e)
          return e.data.who == target
        end, Player.HistoryGame)[1]
        return first_turn and current_turn.id == first_turn.id
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__suoxin-change:"..target.id}) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, id in ipairs(target:getCardIds("h")) do
      room:setCardMark(Fk:getCardById(id), "@@emo__suoxin-inhand", 1)
    end
    target:filterHandcards()
  end,
})

skel:addEffect(fk.DamageInflicted, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and target.chained and #target:getAvailableEquipSlots() > 0
  end,
  on_cost = function (self, event, target, player, data)
    if player.room:askToSkillInvoke(player, {skill_name = skel.name,
     prompt = "#emo__suoxin-reset:"..target.id.."::"..data.damage  }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = target:getAvailableEquipSlots()
    if #choices > 0 then
      local choice = room:askToChoice(player, {choices = choices, skill_name = skel.name, prompt = "#emo__suoxin-slot:"..target.id })
      room:abortPlayerArea(target, choice)
    end
    data:preventDamage()
  end,
})

skel:addEffect("filter", {
  card_filter = function (self, card, player)
    return card:getMark("@@emo__suoxin-inhand") ~= 0
  end,
  view_as = function (self, player, card)
    local c = Fk:cloneCard("iron_chain", card.suit, card.number)
    c.skillName = skel.name
    return c
  end,
})

return skel
